define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yunfood/printer/device/index' + location.search,
                    add_url: 'yunfood/printer/device/add',
                    edit_url: 'yunfood/printer/device/edit',
                    del_url: 'yunfood/printer/device/del',
                    multi_url: 'yunfood/printer/device/multi',
                }
            });

            var table = $("#table");

            // 初始化表格template
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        // 该列为复选框字段,如果后台的返回state值将会默认选中
                        {
                            field: 'state',
                            checkbox: true
                        },
                        // sortable为是否排序,operate为搜索时的操作符,visible表示是否可见
                        {
                            field: 'id',
                            title: 'ID',
                            sortable: true,
                            operate: false
                        },
                        {
                            field: 'shopId',
                            title: __('限定门店'),
                            searchList: Config.shopList,
                            formatter: function (value, row, index) {
                                return row.shop_text;
                            },
                        },
                        {
                            field: 'name',
                            title: __('名称'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                        },
                        {
                            field: 'brand',
                            title: __('品牌'),
                            searchList: Config.brandList,
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'scenes',
                            title: __('打印场景'),
                            searchList: Config.sceneList,
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'note',
                            title: __('备注'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                        },
                        // {
                        //     field: 'online',
                        //     title: __('是否在线'),
                        //     searchList: {0: '否', 1: '是'},
                        //     formatter: Table.api.formatter.label
                        // },
                        // {
                        //     field: 'last_online_time',
                        //     title: __('上次在线时间'),
                        //     sortable: true,
                        //     operate: false,
                        //     formatter: Table.api.formatter.datetime,
                        // },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: Config.statusList,
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'updated_at',
                            title: __('Update time'),
                            sortable: true,
                            operate: false,
                            formatter: Table.api.formatter.datetime,
                        },
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        // 操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            width: '150px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'print',
                                    text: __('测试'),
                                    classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                    icon: 'fa fa-print',
                                    confirm: '确定进行打印测试？',
                                    url: 'yunfood/printer/device/testPrint',
                                    success: function (data, ret) {
                                        console.log('success', data, ret);
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log('error', data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({},this);
                                var table = $(this.table).clone(true);
                                if (row.mute_operate) {
                                    $(table).data('operate-edit', null);
                                    $(table).data('operate-del', null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ],
                ],
                // 快捷搜索,这里可在控制器定义快捷搜索的字段
                search: false,
                // 启用普通表单搜索
                commonSearch: true,
                // 显示切换视图（table/card）按钮
                showToggle: false,
                // 显示导出按钮
                showExport: true,
                // 启用跨页选择
                maintainSelected: true,
                // 启用固定列
                fixedColumns: true,
                // 固定右侧列数
                fixedRightNumber: 1,
                // 可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        config: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
        },
        api: {
            bindevent: function () {
                // 选择品牌
                $(document).on('change', '#c-brand', function () {
                    var value = $(this).val();
                    console.log('brand change', value);
                    if (value === 'feie') {
                        $('#box-feie').removeClass('hide');
                        $('#box-yilianyun').addClass('hide');
                        $('#box-zhongwu').addClass('hide');
                    } else if (value === 'yilianyun') {
                        $('#box-yilianyun').removeClass('hide');
                        $('#box-zhongwu').addClass('hide');
                        $('#box-feie').addClass('hide');
                    } else if (value === 'zhongwu') {
                        $('#box-zhongwu').removeClass('hide');
                        $('#box-feie').addClass('hide');
                        $('#box-yilianyun').addClass('hide');
                    }
                });

                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {  // 渲染的方法
            },
            events: { // 绑定事件的方法
            }
        }
    };
    return Controller;
});
